package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	"net/http"

	"gitee.com/wu-jin-feng/fgin"
	reflectPb "gitee.com/wu-jin-feng/fgin/demos/fconsul_test/server/pb"
)

type DemoServiceServer struct {
	reflectPb.UnimplementedDemoServiceServer
}

func (s *DemoServiceServer) GetScheme(ctx context.Context, req *reflectPb.GetSchemeReq) (*reflectPb.GetSchemeResp, error) {
	// return nil, errors.New("test1 error")
	req1, _ := http.NewRequest("GET", "http://localhost:8013/test", nil)
	req1.Header = fgin.GetCtxToHeader(ctx)
	resp, err := (&http.Client{}).Do(req1)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	respByte, _ := io.ReadAll(resp.Body)
	if req.Info == nil {
		return nil, errors.New("info 不能为空")
	}
	endData := &reflectPb.GetSchemeResp{
		Data: req.Path + ",gogogo",
		Info: &reflectPb.GetSchemeRespInfo{
			Info: fmt.Sprintf("这是一个info,%s。http8013:%s", req.Info.Info, respByte),
			Name: fmt.Sprintf("作者是%s", req.Info.Name),
		},
	}
	return endData, nil
}

func main() {
	f := fgin.Fgin{}
	s := f.GetGrpcServer("./test.yaml")
	reflectPb.RegisterDemoServiceServer(s, &DemoServiceServer{})
	f.RunGrpc(s)
}
